package com.wtx.chapter06;

/**
 * @description: 霍纳算法, P182
 * @author: wtx
 * @createDate: 2020/6/10
 */
public class Horner {

    // arr: 多项式系数数组, 从低到高存储
    public static int solution(int[] arr, int x){

        int res = arr[arr.length-1];
        for (int i = arr.length-2; i >= 0; i--)
            res = x*res + arr[i];
        return res;
    }

    public static void main(String[] args) {

        int[] array = {-5,1,3,-1,2};
        // 计算 2x^4 - x^3 -3x^2 + x - 5
        System.out.println(Horner.solution(array,3)); //160
    }
}
